package com.yty.order.message;

import com.yty.order.util.RedisUtil;
import com.yty.product.message.StockChangedMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(bindings = {@QueueBinding(value = @Queue("product.order"),//自动创建队列
        exchange = @Exchange(value = "product.fanout.productInfoChanged", type = ExchangeTypes.FANOUT))},//绑定到交换机
        containerFactory = "jsonMessageFactory")//指定json解析工厂
@Slf4j
public class ProductMessageReceiver {

    @Autowired
    private RedisUtil redisUtil;

    @RabbitHandler
    public void handler(StockChangedMessage stockChanged) {
        log.info("库存变化：{}", stockChanged);
        //存入redis，方便查询库存
        redisUtil.set(String.format(RedisUtil.STOCK_CHANGED_PATTERN, stockChanged.getProductId()), stockChanged.getStock());
    }
}
